<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container >
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hetongNo">
              <a-input v-model="model.hetongNo" placeholder="请输入合同编号" :disabled=" model.billStatus !='1' && model.billStatus !='0' " ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hetongName">
              <a-input v-model="model.hetongName" placeholder="请输入合同名称" :disabled=" model.billStatus !='1' && model.billStatus !='0' " ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12"  >
            <a-form-model-item label="合同内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hetongContent" >
              <j-editor v-model="model.hetongContent"  style="width:100%;" :disabled=" model.billStatus !='1' && model.billStatus !='0' "/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12"  v-if="model.id">
            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">
              <a-input v-model="model.createBy" placeholder="请输入创建人" :disabled=" model.billStatus !='1' && model.billStatus !='0' "></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.id">
            <a-form-model-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date placeholder="请选择创建日期" v-model="model.createTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" 
              style="width: 100%" :disabled=" model.billStatus !='1' && model.billStatus !='0' "/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.id">
            <a-form-model-item label="更新人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateBy">
              <a-input v-model="model.updateBy" placeholder="请输入更新人" :disabled=" model.billStatus !='1' && model.billStatus !='0' "></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.id">
            <a-form-model-item label="更新日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateTime">
              <j-date placeholder="请选择更新日期" v-model="model.updateTime" :show-time="true" 
              date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled=" model.billStatus !='1' && model.billStatus !='0' " />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="执行部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
              <j-select-depart v-model="model.sysOrgCode" :multi="true" :disabled=" model.billStatus !='1' && model.billStatus !='0' " />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12" >
            <a-form-model-item label="审批节点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shenpiOrdinal">
              <a-input-number v-model="model.shenpiOrdinal" placeholder="请输入审批节点" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12" >
            <a-form-model-item label="合同状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billStatus">
              <j-dict-select-tag type="list" v-model="model.billStatus" dictCode="hetong_status" placeholder="请选择合同状态" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="未盖章附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="file1">
              <j-upload v-model="model.file1"   :disabled="formDisabled"  ></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="盖章附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="file2">
              <j-upload v-model="model.file2"  ></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="合同部门审批" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="oaHetongShenpiTable.loading"
          :columns="oaHetongShenpiTable.columns"
          :dataSource="oaHetongShenpiTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled && ! isFaWu"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
    </a-tabs>
  
    <div align="center"  style="margin-top:10px;" v-if="formShenPi">
      <j-dict-select-tag type="list" v-model="shenpi_status" dictCode="shenpi_action" placeholder="请选择合同状态"  style="width:100px;"/>
      <a-input style="width:500px;" v-model="shenpi_sign_yijian"  placeholder="驳回原因" v-show="shenpi_status=='2'" />
      <a-button :disabled="! EnableShenPi"  @click="submitShenpi" >提交审批</a-button>   
    </div>


  </a-spin>
</template>

<script>

  import { getAction,postAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import store from '@/store'

  export default {
    name: 'OaHetongForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
 
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        model:{
            shenpiOrdinal:1,
            billStatus:"1",
            sysOrgCode:'',
         },

    
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 0,

        validatorRules: {
           hetongNo: [
              { required: true, message: '请输入合同编号!'},
           ],
           hetongName: [
              { required: true, message: '请输入合同名称!'},
           ],
           hetongContent: [
              { required: true, message: '请输入合同内容!'},
           ],
           file1: [
              { required: true, message: '请输入未盖章附件!'},
           ],
        },
        refKeys: ['oaHetongShenpi', ],
        tableKeys:['oaHetongShenpi', ],
        activeKey: 'oaHetongShenpi',
        // 合同部门审批
        oaHetongShenpiTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '所属部门',
              key: 'sysOrgCode',
              type: JVXETypes.departSelect,
              width:"150px",
              placeholder: '请输入${title}',
              defaultValue:'',
              
            },
            {
              title: '节点',
              key: 'jiedianOrdinal',
              type: JVXETypes.popup,
              popupCode:"report_oa_jiedian",
              field:"jiedian_ordinal,jiedian_name",
              orgFields:"jiedian_ordinal,jiedian_name",
              destFields:"jiedianOrdinal,jiedianName",
              width:"80px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '节点名称',
              key: 'jiedianName',
               type: JVXETypes.input,
               disabled:true,
              width:"150px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '审批人',
              key: 'jiedianLeader',
              type: JVXETypes.userSelect,
              width:"80px",
              placeholder: '请输入${title}',
              defaultValue:'',
              
            },
            {
              title: '审批结果',
              key: 'signResult',
              type: JVXETypes.select,
              options:[],
              dictCode:"shenpi_status",
              width:"100px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled:true,
            },
            {
              title: '审批意见',
              key: 'signYijian',
              type: JVXETypes.input,
              width:"160px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled:true,
            },
            {
              title: '审批时间',
              key: 'signTime',
              type: JVXETypes.datetime,
              disabled:true,
              width:"160px",
              placeholder: '请输入${title}',
              defaultValue:'',

            },
            {
              title: '第二次审批结果',
              key: 'signResult2',
              type: JVXETypes.select,
              options:[],
              dictCode:"shenpi_status",
              width:"150px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled:true,
            },
            {
              title: '第二次审批意见',
              key: 'signYijian2',
              type: JVXETypes.input,
              width:"160px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled:true,
            },
            {
              title: '第二次审批时间',
              key: 'signTime2',
              type: JVXETypes.datetime,
              disabled:true,
              width:"160px",
              placeholder: '请输入${title}',
              defaultValue:'',

            },
            {
              title: '审批日志',
              key: 'shenpiLog',
              type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              disabled:true,
            }
          ]
        },

        url: {
          add: "/oa/oaHetong/add",
          edit: "/oa/oaHetong/edit",
          queryById: "/oa/oaHetong/queryById",
          shenpi:'/oa/oaHetong/shenpi',
          oaHetongShenpi: {
            list: '/oa/oaHetong/queryOaHetongShenpiByMainId'
          },
        }

      }
    },

    watch:{
      shenpi_status(newValue, oldValue){
        console.log(newValue, oldValue)
      },
    },

    props: {
    },

    computed: {
     isFaWu(){
      let userinfo = store.getters.userInfo
       let post = userinfo.post

       return post =='法务'
     },

    },

    mounted() {
      if (! this.model.id) {
        let userinfo = store.getters.userInfo
        this.model.sysOrgCode  = userinfo.departIds     
        console.log( 'this.model.sysOrgCode:', this.model.sysOrgCode)
      }
 
    },
    created () {
    },
    methods: {
      //新增方法执行
      addBefore(){
        this.oaHetongShenpiTable.dataSource=[]

        let userinfo = store.getters.userInfo
        console.log('userinfo:',userinfo)

        this.model.sysOrgCode  = userinfo.departIds
        
        getAction("/oa/oaJiedian/listAll",{}).then(res=>{

          let result =res.result 
          result.forEach( item =>{

            let  sysOrgCode=''  //所属部门
            if( item.jiedianOrdinal ==1 ){
              sysOrgCode= userinfo.departIds
            }

            let jdObj ={
              sysOrgCode,
              jiedianOrdinal: item.jiedianOrdinal, //审批节点
              jiedianName: item.jiedianName, //审批节点名称
            }

            this.oaHetongShenpiTable.dataSource.push( jdObj);
          })

        })

      },


      addAfter(data){

        let userinfo = store.getters.userInfo
        data.sysOrgCode  = userinfo.departIds
        this.model.sysOrgCode  = userinfo.departIds
        console.log('this.model.sysOrgCode :',data.sysOrgCode )

      },

      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },

      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }

            this.requestSubTableData(this.url.oaHetongShenpi.list, params, this.oaHetongShenpiTable, res =>{

              //判断是否审批
              if(this.formShenPi){
                //设置允许审批变量
                let ds = this.oaHetongShenpiTable.dataSource
                let shenpiOrdinal = this.model.shenpiOrdinal

                console.log('shenpiOrdinal:', shenpiOrdinal)
                console.log('ds:', ds)

                let jiedianRows= ds.filter( item=>{
                  return item.jiedianOrdinal == shenpiOrdinal &&  item.jiedianLeader == this.userName
                })

                this.EnableShenPi = jiedianRows.length >0  
                if( jiedianRows.length>0){
                  this.shenpi_row  = jiedianRows[0]
                }

                console.log('this.shenpi_row:',  this.shenpi_row)

              }

          })

        

        }
      },

      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          oaHetongShenpiList: allValues.tablesValue[0].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>